<?php

require_once 'helper/DbTable.php';

class User extends DbTable
{
    public static $_TABLE_NAME_ = 'yxb_user';
    public static $required = [
        'user_state',
        'user_indate',
        'user_idcard',
        'user_phone',
        'user_gender',
        'user_birthday',
        'user_name',
        'user_address'
    ];
    public static $members_mapping = [
        'user_state',
        'user_indate',
        'user_idcard',
        'user_phone',
        'user_gender',
        'user_birthday',
        'user_name',
        'user_id',
        'user_address',
        'email',
        'user_wx'
    ];
    public static $relationship = ['yxb_groupship'];
    public static $extends_mapping = ['dept_name','gp_name','gp_id','dept_id'];

    private $user_state;
    private $user_indate;
    private $user_idcard;
    private $user_gender;
    private $user_birthday;
    private $user_name;
    private $user_id;
    private $user_address;
    private $email;
    private $user_wx;
    private $user_phone;
    private $dept_name;
    private $gp_name;
    private $dept_id;
    private $gp_id;

    public function copy()
    {
        // TODO: Implement copy() method.
        $temp = new self();
        foreach (self::$members_mapping as $item) {
            DbTable::callSetMethod($temp, $item, DbTable::callGetMethod($this, $item));
        }
        return $temp;
    }

    public function changeTo($obj)
    {
        // TODO: Implement changeTo() method.
        foreach (self::$members_mapping as $item) {
            DbTable::callSetMethod($this, $item, DbTable::callGetMethod($obj, $item));
        }
    }
    public function checkRelationship()
    {
        $sql = [];
        foreach (self::$relationship as $item) {
            $format = "select * from %s where user_id=%s;";
            array_push($sql, sprintf($format, $item, $this->getUserId()));
        }
        return $sql;

    }
    public static function getTableName()
    {
        // TODO: Implement getTableName() method.
        return self::$_TABLE_NAME_;
    }

    public static function getRequired()
    {
        // TODO: Implement getRequired() method.
        return self::$required;
    }

    public function loadData($data)
    {
        // TODO: Implement loadData() method.
        $temp = array_merge(self::$members_mapping,self::$extends_mapping);
        foreach ($temp as $member) {

            if (array_key_exists($member, $data)) {
                DbTable::callSetMethod($this, $member, $data[$member]);
            } else {
                return false;
            }
        }
        return true;
    }

    public function add()
    {
        // TODO: Implement add() method.
        $format = "insert into %s(%s) values(%s);";
        $attr = [];
        $values = [];
        foreach (self::$members_mapping as $item) {
            $value = DbTable::callGetMethod($this, $item);
            if ($item == "user_id" || $value == null)
                continue;
            array_push($attr, $item);
            is_numeric($value) ? array_push($values, "$value") : array_push($values, "\"$value\"");
        }
        $sql = sprintf($format, self::$_TABLE_NAME_, join(",", $attr), join(",", $values));
        return $sql;
    }

    public function delete()
    {
        // TODO: Implement delete() method.
        $format = "delete from %s where user_id=%s;";

        $sql = sprintf($format, self::$_TABLE_NAME_, $this->getUserId());
        return $sql;
    }

    public function edit($data)
    {
        // TODO: Implement edit() method.
        $format = "update %s set %s where user_id=%s;";
        $changeSql = [];
        foreach (self::$members_mapping as $item) {
            if (isset($data[$item]) && $item != "user_id") {
                DbTable::callsetMethod($this, $item, $data[$item]);
                is_numeric($data[$item]) ? array_push($changeSql, $item . " = " . $data[$item]) : array_push($changeSql, $item . "= \"" . $data[$item] . "\"");;
            }
        }
        $sql = null;
        if (count($changeSql) >= 1)
            $sql = sprintf($format, self::$_TABLE_NAME_, join(",", $changeSql), $this->getUserId());
        return $sql;
    }

    public static function query($condition)
    {
        // TODO: Implement query() method.
        if (isset($condition['name']) && isset($condition['value'])) {
            $format = "select yxb_user.*,_department.gp_name,_department.dept_name from yxb_user left join  
(select yxb_department.dept_name,_dept.user_id,_dept.gp_name from 
(select yxb_groupship.user_id,yxb_group.* from yxb_groupship right join yxb_group on yxb_groupship.gp_id = yxb_group.gp_id) as _dept
 left join yxb_department on yxb_department.dept_id = _dept.dept_id) as _department
on yxb_user.user_id = _department.user_id where yxb_user.%s=%s;";
            $sql = sprintf($format, $condition['name'], $condition['value']);
            return $sql;
        }
        if (isset($condition['page']) && isset($condition['pageSize'])) {
            $page = (int)($condition['page']) - 1;
            $pageSize = (int)($condition['pageSize']);
            if ($page < 0)
                return null;
            $format = "select yxb_user.*,_department.gp_name,_department.dept_name,_department.dept_id,_department.gp_id from yxb_user left join  
(select yxb_department.dept_name,_dept.user_id,_dept.gp_name,_dept.dept_id,_dept.gp_id from 
(select yxb_groupship.user_id,yxb_group.* from yxb_groupship right join yxb_group on yxb_groupship.gp_id = yxb_group.gp_id) as _dept
 left join yxb_department on yxb_department.dept_id = _dept.dept_id) as _department
on yxb_user.user_id = _department.user_id ";
            if(isset($condition['condition']['dept_id'])){
                $format.=" where dept_id = ".$condition['condition']['dept_id'];
            }elseif (isset($condition['condition']['user_name'])){
                $format.=" where user_name = ".$condition['condition']['user_name'];
            }
            if(isset($condition['condition']['dept_id'])&&isset($condition['condition']['gp_id'])){
                $format.=" and gp_id = ".$condition['condition']['gp_id'];
            }

            $format .=" limit %d,%d;";
            $sql = sprintf($format,$page * $pageSize, $pageSize);
            return $sql;
        }
        return null;
    }

    /**
     * @return mixed
     */
    public function getUserState()
    {
        return $this->user_state;
    }

    /**
     * @param mixed $user_state
     */
    public function setUserState($user_state)
    {
        $this->user_state = $user_state;
    }

    /**
     * @return mixed
     */
    public function getUserIndate()
    {
        return $this->user_indate;
    }

    /**
     * @param mixed $user_indate
     */
    public function setUserIndate($user_indate)
    {
        $this->user_indate = $user_indate;
    }

    /**
     * @return mixed
     */
    public function getUserIdcard()
    {
        return $this->user_idcard;
    }

    /**
     * @param mixed $user_idcard
     */
    public function setUserIdcard($user_idcard)
    {
        $this->user_idcard = $user_idcard;
    }

    /**
     * @return mixed
     */
    public function getUserGender()
    {
        return $this->user_gender;
    }

    /**
     * @param mixed $user_gender
     */
    public function setUserGender($user_gender)
    {
        $this->user_gender = $user_gender;
    }

    /**
     * @return mixed
     */
    public function getUserBirthday()
    {
        return $this->user_birthday;
    }

    /**
     * @param mixed $user_birthday
     */
    public function setUserBirthday($user_birthday)
    {
        $this->user_birthday = $user_birthday;
    }

    /**
     * @return mixed
     */
    public function getUserName()
    {
        return $this->user_name;
    }

    /**
     * @param mixed $user_name
     */
    public function setUserName($user_name)
    {
        $this->user_name = $user_name;
    }

    /**
     * @return mixed
     */
    public function getUserId()
    {
        return $this->user_id;
    }

    /**
     * @param mixed $user_id
     */
    public function setUserId($user_id)
    {
        $this->user_id = $user_id;
    }

    /**
     * @return mixed
     */
    public function getUserAddress()
    {
        return $this->user_address;
    }

    /**
     * @param mixed $user_address
     */
    public function setUserAddress($user_address)
    {
        $this->user_address = $user_address;
    }

    /**
     * @return mixed
     */
    public function getEmail()
    {
        return $this->email;
    }

    /**
     * @param mixed $email
     */
    public function setEmail($email)
    {
        $this->email = $email;
    }

    /**
     * @return mixed
     */
    public function getUserWx()
    {
        return $this->user_wx;
    }

    /**
     * @return mixed
     */
    public function getUserPhone()
    {
        return $this->user_phone;
    }

    /**
     * @param mixed $user_phone
     */
    public function setUserPhone($user_phone)
    {
        $this->user_phone = $user_phone;
    }


    /**
     * @param mixed $user_wx
     */
    public function setUserWx($user_wx)
    {
        $this->user_wx = $user_wx;
    }

    /**
     * @return mixed
     */
    public function getDeptName()
    {
        return $this->dept_name;
    }

    /**
     * @param mixed $dept_name
     */
    public function setDeptName($dept_name)
    {
        $this->dept_name = $dept_name;
    }

    /**
     * @return mixed
     */
    public function getGpName()
    {
        return $this->gp_name;
    }

    /**
     * @param mixed $gp_name
     */
    public function setGpName($gp_name)
    {
        $this->gp_name = $gp_name;
    }

    /**
     * @return mixed
     */
    public function getDeptId()
    {
        return $this->dept_id;
    }

    /**
     * @param mixed $dept_id
     */
    public function setDeptId($dept_id)
    {
        $this->dept_id = $dept_id;
    }

    /**
     * @return mixed
     */
    public function getGpId()
    {
        return $this->gp_id;
    }

    /**
     * @param mixed $gp_id
     */
    public function setGpId($gp_id)
    {
        $this->gp_id = $gp_id;
    }


    public function __toString()
    {
        $data = array();
        $temp = array_merge(self::$members_mapping,self::$extends_mapping);
        foreach ($temp as $item) {
            $key = DbTable::upperFirstName($item,true);
            $value = DbTable::callGetMethod($this, $item);
            $data[$key] = $value;
        }
        return json_encode($data);
    }

}